<?php
namespace app\api\controller\v1;
header("Content-type: text/html; charset=utf-8");

use app\common\lib\aes\Aes;
use think\Db;
use app\api\model\User as M;
use think\Log;
use app\common\lib\exception\ApiException;

class User extends Common {

    /**
     * 根据id获取用户信息
     * @param id int  用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function read(){
        $m = new M();
        return $m->read();
    }

    /**
     * 获取用户信息
     * @param id int  用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function get_user_info(){
        $data = $_POST;
        if(empty($data)) {
            throw new ApiException('数据不合法！',401);
        }

        if(!isset($data['student_id']) && !isset($data['teacher_id'])) {
            throw new ApiException('用户id is not found！',404);
        }

        try{
            if(isset($data['student_id'])){
                $result = db('user')
                    ->alias('u')
                    ->join('auth_group auth','u.user_auth=auth.id','LEFT')
                    ->join('student stu','u.id=stu.student_id','LEFT')
                    ->join('grade gra','stu.grade_id = gra.id','LEFT')
                    ->join('address add','stu.student_id = add.member_id','LEFT')
                    ->join('area area','add.area_code = area.code','LEFT')
                    ->join('city city','area.citycode = city.code','LEFT')
                    ->join('student_rank rank', 'stu.student_rank_id=rank.id', 'LEFT')
                    ->field('u.id,u.full_name,u.username,u.nickname,u.tel,u.sex,u.rec_bonus,u.pay_password,u.password,u.token,stu.tuition,stu.coin,stu.student_card_id,stu.student_id,stu.follow_les_id,stu.student_rank_id,rank.student_rank,stu.grade_id,gra.grade,add.address,add.area_code,city.name as city_name,auth.title')
                    ->where('u.id='.$data['student_id'])
                    ->find();

                $obj = new Aes();
                $result['token'] = $obj->encrypt($result['token']."||".$result['student_id']);

                return show(config('code.success'),'ok！',$result);
            }elseif (isset($data['teacher_id'])){
                $result = db('user')
                    ->alias('u')
                    ->join('auth_group auth','u.user_auth=auth.id','LEFT')
                    ->join('teacher tea','u.id=tea.teacher_id','LEFT')
                    ->join('address add','tea.teacher_id = add.member_id','LEFT')
                    ->join('area area','add.area_code = area.code','LEFT')
                    ->join('city city','area.citycode = city.code','LEFT')
                    ->join('teacher_rank rank', 'tea.teacher_rank_id=rank.id', 'LEFT')
                    ->field('u.id,u.full_name,u.username,u.nickname,u.pay_password,u.password,u.pay_password,u.tel,u.token,u.sex,tea.teacher_bonus,tea.teacher_card_id,tea.teacher_id,rank.teacher_rank,u.rec_bonus,tea.teacher_rank_id,add.address,add.area_code,city.name as city_name')
                    ->where('u.id='.$data['teacher_id'])
                    ->find();

                $obj = new Aes();
                $result['token'] = $obj->encrypt($result['token']."||".$result['teacher_id']);

                return show(config('code.success'),'ok！',$result);
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }
    }

    /**
     * 新增用户
     * @request post
     * @author 王柯 <wangke.email@foxmail.com>
     * @return $this
     */
    public function save(){
        $m = new M();
        return $m->addUser();
    }

    /**
     * 修改用户信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function update(){
        $m = new M();
        return $m->edit();
    }

    /**
     * 获取用户数量
     * @author 王柯 <wangke.email@foxmail.com>
     * @return int|string
     */
    public function getUserCount(){
        $m = new M();
        return $m->getUserCount();
    }

    /**
     * 效验支付密码
     * @param pay_password int 支付密码 *必传
     * @param id int 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return void
     */
    public function check_pay_password(){
        $data = $_POST;
        if(empty($data)) {
            throw new ApiException('数据不合法！',401);
        }

        if(!isset($data['id'])){
            throw new ApiException('id is not found！',404);
        }

        if(!isset($data['pay_password'])){
            throw new ApiException('pay_password is not found！',404);
        }

        try{
            $password = db('user')->where('id='.$data['id'])->field('pay_password')->find();

            if($password['pay_password'] != md5($data['pay_password'])){
                return show(config('code.error'),'支付密码错误！');
            }else{
                return show(config('code.success'),'效验成功！');
            }
        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }

    }

    /**
     * 修改用户密码
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function editPassword(){
        $m = new M();
        return $m->editUserPassword();
    }

    /**
     * @ 查询某人推荐的学生或者教师记录
     * @param id int 用户id 必传
     * @param status int 状态 必传 1查询推荐学生记录 2查询推荐教师记录
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */
    public function get_rec_history(){
        $result = (new M)->recHistory();
        return $result;
    }
}